/*
 *  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
 *  reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in
 *  the documentation and/or other materials provided with the
 *  distribution.
 *
 *  3. The end-user documentation included with the redistribution,
 *  if any, must include the following acknowledgment:
 *  "This product includes software developed by the
 *  Sun Microsystems, Inc. for Project JXTA."
 *  Alternately, this acknowledgment may appear in the software itself,
 *  if and wherever such third-party acknowledgments normally appear.
 *
 *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
 *  must not be used to endorse or promote products derived from this
 *  software without prior written permission. For written
 *  permission, please contact Project JXTA at http://www.jxta.org.
 *
 *  5. Products derived from this software may not be called "JXTA",
 *  nor may "JXTA" appear in their name, without prior written
 *  permission of Sun.
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *  SUCH DAMAGE.
 *  ====================================================================
 *
 *  This software consists of voluntary contributions made by many
 *  individuals on behalf of Project JXTA.  For more
 *  information on Project JXTA, please see
 *  <http://www.jxta.org/>.
 *
 *  This license is based on the BSD license adopted by the Apache Foundation.
 *
 *  $Id: NetBeamsPanel.java,v 1.2 2006/07/13 05:26:36 nano Exp $
 */

package net.jxta.myjxta.misc.beam;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.net.URL;
import java.util.Hashtable;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

public class NetBeamsPanel extends JPanel {

    private JPanel cpanel = null;
    private Hashtable sensors = null;
    private int nsensors = 0;
    private MapPanel mp = null;

    public NetBeamsPanel(boolean isMain, Properties nbp) {
        // set insets for the BorderLayout
        setLayout(new BorderLayout(1, 2));

        // dimension is different if standalone or embedded in an app
        if (isMain) {
            setSize(new Dimension(720, 565));
            setPreferredSize(new Dimension(720, 565));
            setMaximumSize(new Dimension(720, 565));
        } else {
            setSize(new Dimension(720, 595));
            setPreferredSize(new Dimension(720, 595));
            setMaximumSize(new Dimension(720, 595));
        }

        // fetch the image map for sfbay
        String mapURLstr = null;
        if (nbp != null) {
            mapURLstr = nbp.getProperty("cached-mapURL");
            if (mapURLstr == null) mapURLstr = nbp.getProperty("mapURL");
        }
        URL imgurl = null;
        try {
            imgurl = new URL(mapURLstr);
            System.out.println(mapURLstr);
        } catch (Exception imgurlexc) {
            imgurlexc.printStackTrace();
        }

        // instantiate bottom status field
        JLabel stat = new JLabel("NetBeams Status");
        stat.setPreferredSize(new Dimension(720, 25));
        stat.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

        // instantiate map panel
        mp = new MapPanel(imgurl, stat);
        if (nbp != null) {
            try {
                double gpsx0 = Double.parseDouble(nbp.getProperty("gpsx0"));
                double gpsy0 = Double.parseDouble(nbp.getProperty("gpsy0"));
                double gpsx1 = Double.parseDouble(nbp.getProperty("gpsx1"));
                double gpsy1 = Double.parseDouble(nbp.getProperty("gpsy1"));
                mp.setGPSUpperLeft(gpsx0, gpsy0);
                mp.setGPSLowerRight(gpsx1, gpsy1);
            } catch (Exception gpsexc) {
                gpsexc.printStackTrace();
            }
        }
        mp.setPreferredSize(new Dimension(375, 540));
        mp.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

        // instantiate chart panel
        cpanel = new JPanel();
        cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.PAGE_AXIS));
        cpanel.setOpaque(true);
        cpanel.setBackground(Color.gray);
        cpanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        //put chart panel in scroll pane
        JScrollPane sp = new JScrollPane(cpanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        //sp.setPreferredSize(new Dimension(400, 600));

        //add components to layout
        add(mp, BorderLayout.WEST);
        add(sp, BorderLayout.EAST);
        add(stat, BorderLayout.SOUTH);

        // add the sensors from the Properties file
        String sidlist = nbp.getProperty("sidlist");
        if (sidlist != null) {
            String[] sid = parseArrayProperty(sidlist, "|");
            if (sid != null && sid.length > 0) {
                for (int i = 0; i < sid.length; i++) {
                    Properties p = getSensorProperties(nbp, sid[i]);
                    addSensor(sid[i], new NetBeamsChart(p));
                    mp.addSensor(p); // add Sensor to the MapPanel;
                }
            }
        }

        // deal with menubar.  If not main app, add one.  Main app adds its own
        NetBeamsMenuBar mb = null;
        if (!isMain) {
            mb = new NetBeamsMenuBar(this);
            mb.setPreferredSize(new Dimension(720, 30));
            mb.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            add(mb, BorderLayout.NORTH);
        }
    }

    public void addSensor(String sid, NetBeamsChart chart) {
        if (sensors == null) {
            sensors = new Hashtable();
        }
        chart.setPreferredSize(new Dimension(325, 140));
        chart.setMaximumSize(new Dimension(325, 140));
        sensors.put(sid, chart);
        nsensors++;

        cpanel.add(chart);

        cpanel.setPreferredSize(new Dimension(325, nsensors * 140));
        cpanel.setMaximumSize(new Dimension(325, nsensors * 140));
        cpanel.revalidate();
        chart.surf.start();
    }

    public NetBeamsChart removeSensor(String sensorID) {
        if (sensors != null) {
            nsensors--;

            //to do list
            //need a way to mp.removeSensor(sid)

            return (NetBeamsChart) sensors.remove(sensorID);
        }
        return null;
    }

    private String[] parseArrayProperty(String str, String delim) {
        StringTokenizer st = new StringTokenizer(str, delim);
        Vector v = new Vector();
        while (st.hasMoreTokens()) {
            v.addElement(st.nextToken());
        }
        String[] retval = new String[v.size()];
        for (int i = 0; i < v.size(); i++) {
            retval[i] = (String) v.elementAt(i);
        }
        return retval;
    }

    private Properties getSensorProperties(Properties mp, String sid) {

        if (mp == null || sid == null) return null;

        Properties p = new Properties();
        p.setProperty("sid", sid);
        p.setProperty("gridid", mp.getProperty("gridid"));
        p.setProperty("vxpath", mp.getProperty("sid-" + sid + "-vxpath"));
        p.setProperty("type", mp.getProperty("sid-" + sid + "-type"));
        p.setProperty("scalefactor", mp.getProperty("sid-" + sid + "-scalefactor"));
        p.setProperty("gpsx", mp.getProperty("sid-" + sid + "-gpsx"));
        p.setProperty("gpsy", mp.getProperty("sid-" + sid + "-gpsy"));
        p.setProperty("units", mp.getProperty("sid-" + sid + "-units"));
        p.setProperty("desc", mp.getProperty("sid-" + sid + "-desc"));
        p.setProperty("url", mp.getProperty("sid-" + sid + "-url"));
        return p;
    }

    public void destroy() {
        // methods to shutdown the panel
        // not implemented yet
        // should iterate through Hashtable of sensors and call surf.stop()
        // should dispose of any graphical components
    }
}
